package com.xiaoyun.xymall.search.controller;

import com.xiaoyun.common.to.es.SkuEsModel;
import com.xiaoyun.xymall.search.service.MallSearchService;
import com.xiaoyun.xymall.search.vo.SearchParam;
import com.xiaoyun.xymall.search.vo.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Controller
public class SearchController {
    @Autowired
    MallSearchService mallSearchService;

    @GetMapping("/list.html")
    public String listPage(SearchParam searchParam, Model model, HttpServletRequest request) {
        searchParam.set_queryString(request.getQueryString());
        SearchResult result = mallSearchService.search(searchParam);
        List<Long> list = new ArrayList<>();

        for (SkuEsModel item : result.getProducts()) {
            List<Long> collect = item.getAttrs().stream().map(SkuEsModel.Attrs::getAttrId).collect(Collectors.toList());
            list.addAll(collect);
        }
        result.setAttrIds(list);

        model.addAttribute("result", result);

        return "list";
    }
}
